class MarriagesController < ApplicationController
  before_filter :login_required
  
  def index
    @character = current_player.player_character
    @marriages_to = Marriage.find_proposals_to(@character) unless @character.nil?
    @marriages_from = Marriage.find_proposals_from(@character) unless @character.nil?
    if @character.ruler?
      @eligible = Character.find_unmarried(@character)
    end
  end
  
  def show
    @pc = current_player.player_character
    @character = Character.find(params[:id])
    raise "Invalid character" unless @character
    @marriage = Marriage.new
    @marriage.character_id = current_player.player_character.id unless current_player.nil? or current_player.player_character.nil?
    @marriage.from = @character.id
    @marriage.dowry = 0
    @potentials = Character.find_unmarried_for(@character)
    respond_to do |format|
      format.html # new.html.erb
    end
  end
  
  def create
    @character = current_player.player_character
    raise "Cannot propose marriage without a player character!" unless @character
    @marriage = Marriage.new(params[:marriage])
    to = @marriage.to_character
    raise "Invalid marriage target" unless to
    from = @marriage.from_character
    valid = true
    error_msg = nil
    saved = false
    
    if from
      if from.married?
        error_msg = "#{from.name_and_title} is married!"
        valid = false
      end
      if to.married?
        error_msg = "#{to.name_and_title} is married!"
        valid = false
      end
      if !from.controlled_by? (@character)
        error_msg = "#{from.name_and_title} is not controlled by you (must be a family member and vassal)!"
        valid = false
      end
    else
      valid = false
      error_msg = "Invalid marriage proposal. No potential spouse specified!"
    end
    
    saved = @marriage.save unless !valid
    from_name_and_title = @marriage.from_character.name_and_title
    to_name_and_title = @marriage.to_character.name_and_title
    respond_to do |format|
      if saved
        flash[:notice] = "Successfully proposed marriage between #{from_name_and_title} and #{to_name_and_title}."
        format.html { redirect_to(to) }
        format.xml  { render :xml => @marriage, :status => :created, :location => @marriage }
      else
        flash[:notice] = error_msg
        format.html { redirect_to(to) }
        format.xml  { render :xml => error_msg, :status => :unprocessable_entity }
      end
    end
    
  end
  
  def update
    @character = current_player.player_character
    raise "Cannot accept marriages without a player character!" unless @character
    @marriage = Marriage.find(params[:id])
    raise "Invalid marriage proposal" unless @marriage
    from_name_and_title = @marriage.from_character.name_and_title
    to_name_and_title = @marriage.to_character.name_and_title
    respond_to do |format|
      if @marriage.accept (@character)
        flash[:notice] = "Accepted proposed marriage between #{from_name_and_title} and #{to_name_and_title}."
        format.html { redirect_to ({:action => "index"}) }
        format.xml  { head :ok }
      else
        flash[:notice] = "Could not accept proposed marriage between #{from_name_and_title} and #{to_name_and_title}."
        format.html { redirect_to ({:action => "index"}) }
        format.xml  { render :xml => @marriage.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def destroy
    @character = current_player.player_character
    raise "Cannot propose marriages without a player character!" unless @character
    @marriage = Marriage.find(params[:id])
    raise "Invalid marriage proposal" unless @marriage
    from_name_and_title = @marriage.from_character.name_and_title
    to_name_and_title = @marriage.to_character.name_and_title
    respond_to do |format|
      if @marriage.character_id == @character.id && @marriage.destroy
        flash[:notice] = "Cancelled proposed marriage between #{from_name_and_title} and #{to_name_and_title}." unless @marriage.rejected
        format.html { redirect_to ({:action => "index"}) }
        format.xml  { head :ok }
      elsif @marriage.reject (@character)
        flash[:notice] = "Rejected proposed marriage between #{from_name_and_title} and #{to_name_and_title}."
        format.html { redirect_to ({:action => "index"}) }
        format.xml  { head :ok }
      else
        format.html { redirect_to ({:action => "index"}) }
        format.xml  { render :xml => @marriage.errors, :status => :unprocessable_entity }
      end
    end
  end
  
end
